package com.codedancer.campus.mapper;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.core.BaseMapper;
import com.codedancer.campus.po.HeadlineNews;
import com.codedancer.campus.qo.headlineNews.HeadlineNewsPageSearchQo;

public interface HeadlineNewsMapper extends BaseMapper<HeadlineNews> {

    /**
     * 分页查询头条新闻(客户端)
     *
     * @param headlineNewsPage
     * @return
     */
    default IPage<HeadlineNews> page2Client(Page<HeadlineNews> headlineNewsPage,
        HeadlineNewsPageSearchQo headlineNewsPageSearchQo) {
        LambdaQueryWrapper<HeadlineNews> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(HeadlineNews::getPublishDate);
        lambdaQueryWrapper.eq(HeadlineNews::getType, headlineNewsPageSearchQo.getType());
        if (ObjectUtil.isNotEmpty(headlineNewsPageSearchQo.getLocation())) {
            lambdaQueryWrapper.eq(HeadlineNews::getLocation,
                headlineNewsPageSearchQo.getLocation());
        }
        if (ObjectUtil.isNotEmpty(headlineNewsPageSearchQo.getKeywords())) {
            lambdaQueryWrapper.and(
                query -> query.like(HeadlineNews::getTitle, headlineNewsPageSearchQo.getKeywords())
                    .or()
                    .like(HeadlineNews::getDescription, headlineNewsPageSearchQo.getKeywords()));
        }
        return selectPage(headlineNewsPage, lambdaQueryWrapper);
    }

    /**
     * 分页查询头条新闻（管理端）
     *
     * @param headlineNewsPage
     * @param headlineNewsPageSearchQo
     * @return
     */
    default IPage<HeadlineNews> page2Manager(Page<HeadlineNews> headlineNewsPage,
        HeadlineNewsPageSearchQo headlineNewsPageSearchQo) {
        LambdaQueryWrapper<HeadlineNews> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(headlineNewsPageSearchQo.getKeywords())) {
            lambdaQueryWrapper.and(
                query -> query.like(HeadlineNews::getTitle, headlineNewsPageSearchQo.getKeywords())
                    .or()
                    .like(HeadlineNews::getDescription, headlineNewsPageSearchQo.getKeywords()));
        }
        if (ObjectUtil.isNotEmpty(headlineNewsPageSearchQo.getType())) {
            lambdaQueryWrapper.eq(HeadlineNews::getType, headlineNewsPageSearchQo.getType());
        }
        lambdaQueryWrapper.orderByDesc(HeadlineNews::getPublishDate);
        return selectPage(headlineNewsPage, lambdaQueryWrapper);
    }
}
